package com.chenfan.mcn.controller;

import cn.hutool.core.lang.Assert;
import com.chenfan.mcn.constant.NumberCst;
import com.chenfan.mcn.enums.OperateLockEnum;
import com.chenfan.mcn.extension.Res;
import com.chenfan.common.extension.lock.DistributedLockTemplate;
import com.chenfan.mcn.service.LiveContactOfflineCommissionService;
import com.chenfan.privilege.common.config.UrlAuth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 直播合同-线下佣金
 * @author wulg
 * @date 2023/6/1
 */
@Slf4j
@RestController
@RequestMapping("/liveContract/offlineCommission/")
public class LiveContractOfflineCommissionController {

    @Autowired
    private LiveContactOfflineCommissionService liveContactOscService;

    /**
     * 修改线下佣金结算状态
     *
     * @param incomeContractId  收入合同ID
     * @param status 操作类型：1-提起审批、2-撤回
     * @return {@link Res <Object>}
     */
    @GetMapping("/changeStatus")
    @UrlAuth({"revenueContract_submitOff","revenueContract_withdrawOff"})
    public Res<Long> changeStatus(@RequestParam(required = false) Long incomeContractId, @RequestParam(required = false) Integer status) {
        Assert.notNull(incomeContractId, "收入合同ID不能为空");
        Assert.notNull(status, "操作类型不能为空");
        Assert.isTrue(NumberCst.ONE.equals(status) || NumberCst.TWO.equals(status), "请正确标记操作类型");
        return DistributedLockTemplate.execute(OperateLockEnum.INCOME_CONTRACT.generateKey(incomeContractId),
                () -> Res.ok(liveContactOscService.changeStatus(incomeContractId, status))
        );
    }
}
